package com.gome.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.gome.entity.Role;
import com.gome.entity.RoleMenu;
import com.gome.entity.User;
import com.gome.entity.UserRole;
import com.gome.mapper.RoleMapper;
import com.gome.mapper.RoleMenuMapper;
import com.gome.mapper.UserMapper;
import com.gome.mapper.UserRoleMapper;
import com.gome.utils.DateUtils;
import com.gome.utils.Result;
import com.gome.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RoleService  {
    @Autowired
    private RoleMapper RoleMapper;
    @Autowired
    private RoleMenuMapper RoleMenuMapper;
    @Autowired
    private BMService BMService;
    @Autowired
    private UserRoleMapper UserRoleMapper;
    @Autowired
    private UserMapper UserMapper;
    public List<Role>  queryRoles(Map map) {

        List<Role> roles = RoleMapper.queryRoles(map);

        return roles;
    }

    public Role queryRoleWithMenu(String ocRolebm ,Boolean queryUserFlag) {
        Role role = RoleMapper.queryRoleWithMenu(ocRolebm);

        if(queryUserFlag){
            Wrapper<UserRole> wrapper=new EntityWrapper<UserRole>();
            wrapper.eq("oc_rolebm",role.getOcRolebm());
            wrapper.eq("oc_yxbz", 1);
            wrapper.setSqlSelect("oc_user_bm  ocUserbm");
            List<UserRole> userRoles = UserRoleMapper.selectList(wrapper);
            List<User> userList=new ArrayList<User>();
            User user= new User();
            for(UserRole ur:userRoles){
                user.setOcYhbm(ur.getOcUserbm());
                //切记主键不要用int
                user = UserMapper.selectOne(user);
                userList.add(user);
            }
        role.setUsers(userList);
        }
        return  role;
    }

    public Result addRole(Role role, String addMenubms) {
        //新增角色菜单
        //校验菜单名称是否重复
        Role r= RoleMapper.queryRoleByRole(role);
        if(r!=null)  return new Result(ResultCode.FAIL, "角色名称["+role.getVcRolemc()+"]已存在",  null);
        String rolebm = BMService.createBM("ROLE" + DateUtils.formatDate(new Date(), DateUtils.YYMM), "00000", "sys_role", role.getOcCzy());
        role.setDtCzrq(new Date());
        role.setOcYxbz("1");
        role.setOcZt("1");
        role.setOcRolebm(rolebm);
        RoleMapper.addRole(role);
        //关联菜单
        List<String> list = JSONObject.parseObject(addMenubms, List.class);
        if(null!=list&&list.size()>0){
            RoleMenu rm=new RoleMenu();
            for(String Menubm:list){
                rm.setOcRolebm(rolebm);
                rm.setOcMenubm(Menubm);
                rm.setOcYxbz("1");
                rm.setDtCzrq(new Date());
                rm.setOcCzy(role.getOcCzy());
                RoleMenuMapper.addRoleMenu(rm);
            }
            return new Result(ResultCode.SUCCESS, "添加角色成功", null);
        }else{
            return new Result(ResultCode.SUCCESS, "添加角色成功,未添加权限菜单", null);
        }


    }

    public Result deleteRole(String ocRolebm) {
        //固定报表 这个需要单独处理？？

        //删除用户-角色关联
        Wrapper<UserRole> wrapper=new EntityWrapper<UserRole>();
        wrapper.where("oc_rolebm={0}",ocRolebm);
        UserRoleMapper.delete(wrapper);
        //删除角色-菜单关联
        RoleMenuMapper.deleteRoleMenuByRolebm(ocRolebm);
        //删除角色
        RoleMapper.deleteRole(ocRolebm);
        return new Result(ResultCode.SUCCESS, "删除角色成功",null );
    }



    public Result updateRole(Role role, String menubms) {
        Role r= RoleMapper.queryRoleByRole(role);
        if(r!=null)  return new Result(ResultCode.FAIL, "角色名称["+role.getVcRolemc()+"]已存在",  null);
        role.setDtCzrq(new Date());
        RoleMapper.updateRole(role);
        //删除角色现有菜单权限
        RoleMenuMapper.deleteRoleMenuByRolebm(role.getOcRolebm());
        //添加角色新菜单权限
        List<String> list = JSONObject.parseObject(menubms, List.class);
        if(null!=list&&list.size()>0){
            RoleMenu rm=new RoleMenu();
            for(String Menubm:list){
                rm.setOcRolebm(role.getOcRolebm());
                rm.setOcMenubm(Menubm);
                rm.setOcYxbz("1");
                rm.setDtCzrq(new Date());
                rm.setOcCzy(role.getOcCzy());
                RoleMenuMapper.addRoleMenu(rm);
            }
            return new Result(ResultCode.SUCCESS, "修改角色成功", null);
        }else{
            return new Result(ResultCode.SUCCESS, "修改角色成功,未添加权限菜单", null);
        }
    }

    public List<Role> getValidRoles() {
        List<Role> roles = RoleMapper.queryValidRoles();

        return roles;
    }
}
